from qiniu import Auth, put_file
from rest_framework.views import APIView
from rest_framework.response import Response


class QiniuUtils:
    def __init__(self):
        self.AK = "i4AZwb8GrukBN4x82amxQwPOwMPHt-7Fphle9fwI"
        self.SK = "PREeYGiY2QU5rT3gw1DC1FJ38omMU0ZKAmI7lzLR"
        self.q = Auth(self.AK, self.SK)
        self.Bucket_Name = "h2410b"

    # 用于上传图片并生成图片地址
    def upload_file(self, localfile, time):
        try:
            # 处理图片路径,获取图片名称
            if "\\" in localfile:
                key = localfile.split('\\')[-1]
            elif "/" in localfile:
                key = localfile.split('/')[-1]
            else:
                key = localfile
            # 是否添加过期时间,不添加则为None
            if time is None:
                token = self.q.upload_token(self.Bucket_Name, key)
            else:
                token = self.q.upload_token(self.Bucket_Name, key, time)
            info = put_file(token, key, localfile, version='v2')
            # url = "http://ssdiqdo49.hb-bkt.clouddn.com/" + key
            url = "http://sttogjaer.hb-bkt.clouddn.com/" + key
            return {"url": url, "key": key, "info": info, "code": 200}
        except Exception as e:
            return {"code": 400, "msg": str(e)}

    def qiniu_token(self, filename):
        token = self.q.upload_token(self.Bucket_Name, filename, 3600)
        return token


class QnToken(APIView):
    def __init__(self):
        super().__init__()
        self.qi = QiniuUtils()

    def get(self, request):
        # 这里可以根据需求传入合适的文件名
        token = self.qi.qiniu_token('example_filename')
        return Response({"code": 10000, 'data': token})


qi = QiniuUtils()

# 右键启动好使
# class QiniuUtils:
#     def __init__(self):
#         self.AK = "i4AZwb8GrukBN4x82amxQwPOwMPHt-7Fphle9fwI"
#         self.SK = "PREeYGiY2QU5rT3gw1DC1FJ38omMU0ZKAmI7lzLR"
#         self.q = Auth(self.AK, self.SK)
#         self.Bucket_Name = "h2410b"
#
#     # 用于上传图片并生成图片地址
#     def upload_file(self, localfile, time):
#         try:
#             # 处理图片路径,获取图片名称
#             global str1
#             if "\\" in localfile:
#                 str1 = localfile.split('\\')[-1]
#             elif "/" in str:
#                 str1 = localfile.split('/')[-1]
#             key = str1
#             # 是否添加过期时间,不添加则为None
#             if time is None:
#                 token = self.q.upload_token(self.Bucket_Name, key)
#             else:
#                 token = self.q.upload_token(self.Bucket_Name, key, time)
#             info = put_file(token, key, localfile, version='v2')
#             url = "http://ssdiqdo49.hb-bkt.clouddn.com/" + key
#             return {"url": url, "key": key, "info": info, "code": 200}
#         except  Exception as e:
#             return {"code": 400, "msg": str(e)}
#
#     def qiniu_token(self, filename):
#         token = self.q.upload_token(self.Bucket_Name, filename, 3600)
#         return token
#
#
# qi = QiniuUtils()
# # data = qi.upload_file("C:\\Users\\46506\\Pictures\\联想截图\\截图.png", 3600)
# # print(data)
